package com.ys.malini.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ys.malini.dao.ICommodityDao;
import com.ys.malini.model.TProduct;
import com.ys.malini.model.TProductDetail;

public class CommodityDaoImpl extends HibernateDaoSupport implements ICommodityDao {

	public CommodityDaoImpl() {

	}

	public TProduct saveProduct(TProduct product) {
		getHibernateTemplate().saveOrUpdate(product);
		return product;
	}

	public List findAllProducts() {
		String hsql = "From TProduct ";
		List list = (ArrayList) getHibernateTemplate().find(hsql);
		return list;
	}


	public TProductDetail findProductById(String commodityId) {
		String hsql = "from TProductDetail where productId = '"+commodityId +"'";
		TProductDetail productDetail = (TProductDetail) getHibernateTemplate().find(hsql).get(0);
		return productDetail;
	}

	public List findCommodityById(String commodityId) {
		String hsql = "select a.productId,a.productName,a.color,a.brand,b.stockNum,a.title,a.series," +
				"a.material,a.productStatus,a.productType,a.isNew,a.bigClassType,a.midClassType," +
				"b.productCode,b.productBriefCode,b.productBarCode,b.areaCode,b.rank,b.size," +
				"b.specCode,b.costPrice,b.marketPrice,b.salePrice,b.barginPrice," +
				"b.saleDate,b.produceDate,b.productInfo from TProduct a,TProductDetail b where " +
				"a.productId = b.productId and a.productId = '"+commodityId +"'";
		List list = getHibernateTemplate().find(hsql);
		return list;
	}
	
	public List findComInfoById(String commodityId) {
		String hsql = "select a.productId,a.productName,a.color,b.size," +
				"b.salePrice,b.marketPrice,i.bigimages from TProduct a,TProductDetail b,TImages i where " +
				" a.productId = b.productId and a.productId=i.productId and a.productId = '"+commodityId +"' group by i.productId ";
		System.out.println("===="+hsql);
		List list = getHibernateTemplate().find(hsql);
		System.out.println("list.size===>"+list.size());
		return list;
	}
	
	public TProductDetail saveProductDetail(TProductDetail productDetail) {
		getHibernateTemplate().saveOrUpdate(productDetail);
		return productDetail;
	}
	
	public void removeCommodityById(String commodityId){
		String hql1 = "delete TProduct where 1=1  and productId="+commodityId;
		String hql2 = "delete TProductDetail where 1=1  and productId="+commodityId;
		String hql3 = "delete TImages WHERE 1=1 and productId ="+commodityId;
		Query query1 = getSession().createQuery(hql1);
		query1.executeUpdate();
		Query query2 = getSession().createQuery(hql2);
		query2.executeUpdate();
		Query query3 = getSession().createQuery(hql3);
		query3.executeUpdate();
	}
	public void deteleDetailCommodityById(String commodityId){
		String hql = "delete TProductDetail where 1=1  and productId="+commodityId;
		Query query = getSession().createQuery(hql);		
		query.executeUpdate();
	}
	
	public List queryCommodityDes(String bigClass){
		String hql = "select productId,productName from TProduct where  bigClassType='"+bigClass+ "'";
		List list = getHibernateTemplate().find(hql);
		return list;
	}
	public TProductDetail updateSubProduct(String subProductId,String productId){
		String hql = "UPDATE TProductDetail SET parantProdId ='"+productId+" WHERE productId='"+subProductId+"'";
		TProductDetail productDetail = (TProductDetail) getHibernateTemplate().find(hql).get(0);
		return productDetail;
	}
}
